package com.ats.orderknot.client;

import java.text.ParseException;

import com.bradrydzewski.gwt.calendar.client.Appointment;
import com.bradrydzewski.gwt.calendar.client.AppointmentStyle;
import com.bradrydzewski.gwt.calendar.client.Calendar;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.i18n.shared.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class AppointmentEditor extends DialogBox {
	private TextBox appTitle;
	private TextBox appStartDate;
	private TextBox appEndDate;
	private TextArea appDescription;

	private final Calendar calendar;

	public AppointmentEditor(Calendar calendar) {
		this.calendar = calendar;
		this.setText("Wydazenie");
		FlexTable appEditor = new FlexTable();
		appEditor.setCellSpacing(6);
		FlexCellFormatter cellFormatter = appEditor.getFlexCellFormatter();
		// Add a title to the form
		appEditor.setHTML(0, 0, "Wydazenie");
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		// Add some standard form options
		appEditor.setHTML(1, 0, "Tytyul:");
		appTitle = new TextBox();
		appEditor.setWidget(1, 1, appTitle);

		appEditor.setHTML(2, 0, "Poczatek wyrazenia:");
		appStartDate = new TextBox();
		appEditor.setWidget(2, 1, appStartDate);

		appEditor.setHTML(3, 0, "Koniec wydazenia:");
		appEndDate = new TextBox();
		appEditor.setWidget(3, 1, appEndDate);

		appEditor.setHTML(4, 0, "Opis wydazenia:");
		appDescription = new TextArea();
		appDescription.setVisibleLines(5);
		appEditor.setWidget(4, 1, appDescription);

		final AppointmentEditor THIS = this;

		// Add a close button at the bottom of the dialog
		Button saveButton = new Button("Zapisz", new ClickHandler() {
			public void onClick(ClickEvent event) {
				Appointment appt = new Appointment();
				appt.setStart(OrderKnot.df.parse(THIS.getAppStartDate().getText()));
				appt.setEnd(OrderKnot.df.parse(THIS.getAppEndDate().getText()));
				appt.setTitle(THIS.getAppTitle().getText());
				appt.setDescription(THIS.getAppTitle().getText());
				appt.setStyle(AppointmentStyle.BLUE);
				THIS.getCalendar().addAppointment(appt);
				System.out.println(appt.getTitle());
				System.out.println(appt.getStart().toString());
				System.out.println(appt.getEnd().toString());
				THIS.hide();
				THIS.clear();
			}
		});
		appEditor.setWidget(5, 1, saveButton);
		cellFormatter.setHorizontalAlignment(5, 1,
				HasHorizontalAlignment.ALIGN_RIGHT);

		Button closeButton = new Button("Anuluj", new ClickHandler() {
			public void onClick(ClickEvent arg0) {
				THIS.hide();
				THIS.clear();
			};
		});
		appEditor.setWidget(5, 0, closeButton);
		cellFormatter.setHorizontalAlignment(5, 0,
				HasHorizontalAlignment.ALIGN_LEFT);

		
		this.setWidget(appEditor);
	}

	@Override
	public void show() {
		super.show();
		appTitle.setFocus(true);
	}

	public TextBox getAppTitle() {
		return appTitle;
	}

	public TextBox getAppStartDate() {
		return appStartDate;
	}

	public TextBox getAppEndDate() {
		return appEndDate;
	}

	public TextArea getAppDescription() {
		return appDescription;
	}

	public Calendar getCalendar() {
		return calendar;
	}

	public void clear() {
		appTitle.setText("");
		appStartDate.setText("");
		appEndDate.setText("");
		appDescription.setText("");
	}

}
